package com.example.impl;

import com.example.mapper.OrderMapper;
import com.example.mapper.WxbGoodsMapper;
import com.example.mapper.WxbGoodsSkuMapper;
import com.example.model.WxbGood;
import com.example.model.WxbGoodSku;
import com.example.model.WxbMemeber;
import com.example.model.WxbOrder;
import com.example.util.OrderService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private WxbGoodsMapper wxbGoodsMapper;

    @Autowired
    private WxbGoodsSkuMapper wxbGoodsSkuMapper;

    @Override
    public WxbOrder insertOrder(WxbOrder order) throws Exception {

       String  orderTd= System.currentTimeMillis()+""+(int)(Math.random()*10000000)+1000000;
        order.setOrderId(orderTd);
        order.setOrderTime(new Date());
        WxbMemeber wxbMemeber = (WxbMemeber) SecurityUtils.getSubject().getPrincipal();
        order.setUserId(wxbMemeber.getMemeberId());
        orderMapper.insertSelective(order);
        return order;
    }


    @Override
    public List<WxbOrder> findAll(String customerId) {


       List<WxbOrder> orders = orderMapper.findByCustomerId(customerId);
        for (WxbOrder order : orders) {
            Example example = new Example(WxbGoodSku.class);
            example.createCriteria().andEqualTo("goodId",order.getGoodId());
            WxbGood good =  wxbGoodsMapper.selectByPrimaryKey(order.getGoodId());
            List<WxbGoodSku> skuList = wxbGoodsSkuMapper.selectByExample(example);
            order.setGoodName(good.getGoodName());
            order.setSku(skuList);
            order.setSkuString(getSkuList(skuList));
        }
        return orders;

    }

    @Override
    public List<WxbOrder> findByAll(String userId) {
        Example example = new Example(WxbOrder.class);
        example.createCriteria().andEqualTo("userId",userId);
        List<WxbOrder> orders = orderMapper.selectByExample(example);
        for (WxbOrder order : orders) {
            Example example2 = new Example(WxbGoodSku.class);
            example2.createCriteria().andEqualTo("goodId",order.getGoodId());
            List<WxbGoodSku> skuList = wxbGoodsSkuMapper.selectByExample(example2);
            WxbGood good =  wxbGoodsMapper.selectByPrimaryKey(order.getGoodId());
            order.setGoodName(good.getGoodName());
            order.setSku(skuList);
            order.setSkuString(getSkuList(skuList));
        }
        return orders;
    }

    @Override
    public void updateState(int state, List<String> ids) throws Exception {
        for(String oid:ids){
            orderMapper.updateState(state,oid);
        }
    }

    @Override
    public WxbOrder findById(Long oid) {
        WxbOrder order =orderMapper.selectByPrimaryKey(oid);
        Example example2 = new Example(WxbGoodSku.class);
        example2.createCriteria().andEqualTo("goodId",order.getGoodId());
        WxbGood good =  wxbGoodsMapper.selectByPrimaryKey(order.getGoodId());
        order.setGoodPic(good.getGoodPic());
        order.setGoodName(good.getGoodName());
        List<WxbGoodSku> skuList = wxbGoodsSkuMapper.selectByExample(example2);
        order.setSku(skuList);
        order.setSkuString(getSkuList(skuList));
        return order;
    }

    @Override
    public void delect(Long oid) {

            orderMapper.deleteById(oid);
    }

    @Override
    public WxbOrder findAndId(Long oid) {
        WxbOrder order = orderMapper.selectByPrimaryKey(oid);
        return order;
    }

    @Override
    public void updateAlipayState(String orderId) {
        orderMapper.updateAlipayState(orderId);
    }

    private String getSkuList(List<WxbGoodSku> wxbGoodSku){
        StringBuffer sb = new StringBuffer();
        for (WxbGoodSku sku : wxbGoodSku) {
            sb.append(sku.getSkuName()+","+sku.getSkuPrice()+","+sku.getSkuPmoney()+".");
        }
        return sb.toString();
    }
}
